/**
 * @Author      : maolin
 * @Date        : 2021/3/17
 * @Version     : 1.0
 * @Description : 处理数据业务逻辑
 **/
import {GET_STAFF_LIST, GET_DEPARTMENT_TREE} from "./api";


/**
 * 组合员工和部门为一棵树
 * @param node
 * @returns {Promise<[]>}
 */
export async function getDeptUserTree ( node ) {
  let nodeData = [];
  const staffList = (await GET_STAFF_LIST({pageIndex: 1, pageSize: 1000})).data.list;
  const departmentTree = (await GET_DEPARTMENT_TREE()).data.list;
  nodeData = generateDeptUserTree(staffList, departmentTree, {
    labelKey: 'name',
    valueKey: 'id',
    childrenKey: 'childs',
    idKey: 'id',
    nameKey: 'name',
    relateKey: 'departmentId'
  });
  console.log('nodeData');
  console.log(nodeData);
  let data = [];
  if (node && node.data) {
    let id = node.data.id;
    data = getFromTreeById(id, nodeData);
    return data;
  }
  return nodeData;
}

/**
 * 递归组合员工以及部门树
 * @param staffList
 * @param departmentTree
 * @param opt     字段的配置项
 * @returns {*}
 */
function generateDeptUserTree(staffList, departmentTree, opt) {
  let label = opt && opt.labelKey ? opt.labelKey : 'label';
  let value = opt && opt.valueKey ? opt.valueKey : 'value';
  let children = opt && opt.childrenKey ? opt.childrenKey : 'value';
  let listId = opt && opt.idKey ? opt.idKey : 'id';
  let listName = opt && opt.nameKey ? opt.nameKey : 'name';
  let listRelate = opt && opt.relateKey ? opt.relateKey : 'departmentId';
  let newOpt = {labelKey: label, childrenKey: children, valueKey: value, idKey: listId, nameKey: listName, relateKey: listRelate};
  return _rec(staffList, departmentTree, newOpt);
  function _rec (list, treeData, option) {
    for (let i = 0; i < treeData.length; i ++) {
      treeData[i].nodeId = treeData[i][option.valueKey];
      if (list && list.length > 0) {
        for (let j in list) {
          let item = list[j];
          if (!treeData[i]._isLeaf && item[option.relateKey] == treeData[i][option.valueKey]) {
            if (treeData[i][option.childrenKey] === undefined || !treeData[i][option.childrenKey]) {
              treeData[i][option.childrenKey] = [];
            }
            let staffRow = {};
            staffRow[option.labelKey] = item[option.nameKey];
            staffRow[option.valueKey] = treeData[i][option.valueKey].toString() + '-' + item[option.idKey].toString();
            staffRow.nodeId = staffRow[option.valueKey];
            staffRow._isLeaf = true;
            staffRow._id = item[option.idKey];
            staffRow.userId = item[option.idKey];
            treeData[i][option.childrenKey].push(staffRow);
          }
        }
      }
      if (treeData[i][option.childrenKey] && treeData[i][option.childrenKey].length > 0) {
        treeData[i][option.childrenKey] = _rec(list, treeData[i][option.childrenKey], option);
      }
    }
    return treeData;
  }
}

/**
 * 通过id获取树中的item的子项
 * @param id
 * @param tree
 * @returns {*|*[]}
 */
function getFromTreeById(id, tree, valueKey) {
  let item = null;
  return _rec(id, tree, valueKey);
  function _rec(id, treeData, valueKey) {
    if (valueKey === undefined) {
      valueKey = 'id';
    }
    if (treeData && treeData.length > 0) {
      for (let i in treeData) {
        if (treeData[i] !== undefined && treeData[i][valueKey] == id) {
          item = treeData[i].childs || [];
          break;
        }
        if (treeData[i].childs && treeData[i].childs.length > 0) {
          treeData[i].childs = _rec(id, treeData[i].childs, valueKey);
        }
      }
    }
    return item;
  }
}

export function getItemFromTreeById(id, tree, valueKey) {
  let item = null;
  return _rec(id, tree, valueKey);
  function _rec(id, treeData, valueKey) {
    if (valueKey === undefined) {
      valueKey = 'id';
    }
    if (treeData && treeData.length > 0) {
      for (let i in treeData) {
        if (treeData[i] !== undefined && treeData[i][valueKey] == id) {
          item = treeData[i] || null;
          break;
        }
        if (treeData[i].childs && treeData[i].childs.length > 0) {
          _rec(id, treeData[i].childs, valueKey);
        }
      }
    }
    return item;
  }
}
